﻿@{
    Layout = "_Default";
    ViewData["Title"] = "_Default";
}
@section css {
    <link href="~/css/fa.css" rel="stylesheet" />
}
@section javascript {
    <script>
        $(function () {
            //1.初始化Table
            var oTable = new TableInit();
            oTable.Init();

            $('#btn_query').on('click', function () {
                $("#TableMenu").bootstrapTable('refresh');
            });
            $('#btn_repeat').on('click', function () {
                $("#txt_menus_name").val("");
                $("#TableMenu").bootstrapTable('refresh');
            });

            $('.btn_add').on('click', function () {
                SelectMenu();
                edittype = true;
                $('#myModalLabel').html('菜单添加窗口');
                $("#dialogNew").find(".form-control").val("");  //清空
                $('#menuselect').selectpicker('val', 0);
                $('#menutype').selectpicker('val', 0);
                $('#menuID').val(0);
                $('#dialogNew').modal();
            });

            $('.btn_delete').on('click', function () {
                var data = $("#TableMenu").bootstrapTable('getSelections');
                if (data.length > 0) {
                    bootbox.confirm({
                        message: "确认删除吗？",
                        buttons: {
                            confirm: {
                                label: '确认',
                                className: 'btn-primary'
                            },
                            cancel: {
                                label: '取消',
                                className: 'btn-default'
                            }
                        },
                        callback: function (result) {
                            if (result) {
                                DeleteSubmit(data);
                            }
                        }
                    });
                }
                else {
                    toastr.error("请选中一行后删除！");
                }
            });

            $('#btnSubmit').click(function () {

                if (parseInt($('#type').val()) == 1 && parseInt($('#menuselect').val()) == 0) {
                    toastr.error("未选择父级菜单");

                } else if (parseInt($('#type').val()) == 0 && parseInt($('#menuselect').val()) != 0) {
                    toastr.error("菜单类型与所属父级菜单不匹配");
                }
                else {
                    var requesturl = '/api/Navigator/add';
                    if (!edittype)
                        requesturl = '/api/Navigator/edit';
                    $('#dialogNew').modal('hide');
                    $.ajax({
                        type: 'put',
                        url: requesturl,
                        contentType: 'application/json',
                        data: JSON.stringify({ "name": $('#name').val(), "icon": $('#icon').val(), "url": $('#url').val(), "no": parseInt($('#order').val()), "menuid": parseInt($('#menuselect').val()), "state": parseInt($('#menutype').val()), "explain": $('#explain').val(), "id": parseInt($('#menuID').val()) }),
                        datatype: 'json',
                        success: function (res) {

                            if (res.state == 200) {
                                $("#TableMenu").bootstrapTable('refresh');
                                toastr.success(res.message);
                            }
                            else {
                                toastr.error(res.message);
                            }
                        },
                        error: function (e) {
                            toastr.error(e.responseText);
                        }
                    });
                }
            });

            $('.tb_edit').click(function () {
                var data = $("#TableMenu").bootstrapTable('getSelections');
                if (data.length == 1) {
                    if (data[0].id != 0)
                        edit(data[0].id);
                } else {
                    toastr.error("请选中一行后编辑！");
                }
            });
            var $btnPickIcon = $('#btnIcon');
            var $dialog = $('#dialogNew');
            var $dialogNew = $dialog;
            var $dialogIcon = $('#dialogIcon');
            var $inputIcon = $('#icon');
            var $pickIcon = $('#pickIcon');
            $btnPickIcon.on('click', function () {
                $dialogNew.find('[data-toggle="LgbValidate"] [aria-describedby]').tooltip('hide');
                $dialogNew.hide();
                var icon = $inputIcon.val();
                if (icon) $pickIcon.attr('class', icon);
                $dialogIcon.show();
                //$scroll.resize();
            });

            $dialogIcon.find('div.modal-header, div.modal-footer').on('click', 'button', function () {
                $dialogIcon.hide();
                $dialogNew.show();
            });

            $dialogIcon.find('div.modal-footer').on('click', 'button:last', function () {
                var icon = $pickIcon.attr('class');
                $inputIcon.val(icon);
                $btnPickIcon.find('i').attr('class', icon);
            });
            var $scroll = null;

            $.ajax({
                method: "get",
                async: true,
                url: '/Power/IconView/',
                contentType: 'text/html',
                dataType: 'html',
                success: function (result) {

                    var $html = $dialogIcon.find('.modal-body').html(result);
                    var $iconList = $('div.fontawesome-icon-list').on('click', 'div.fa-hover a, ul li', function () {
                        $pickIcon.attr('class', $(this).find('i, span:first').attr('class'));
                        return false;
                    });
                    $iconList.find('ul li').addClass('col-xl-2 col-md-3 col-sm-4 col-6');
                    $iconList.find('div').addClass('col-xl-2 col-6');
                    $('[data-spy="scroll"]').each(function () {
                        $(this).scrollspy({ target: $(this).attr('data-target') });
                    });

                    //  if (!$.browser.versions.ios) $scroll = $html.find('.fa-nav .nav').overlayScrollbars({ className: 'os-theme-light', scrollbars: { autoHide: 'leave' } });
                },
                error: function (e) {
                    toastr.error(e.responseText);
                }
            });

            //$('#btnIcon').click(function () {
            //     $('#dialogNew').modal('hide');
            //    $('#dialogIconNew').modal();

            //});

            SelectMenu();
        });

        var edittype = true;

        function SelectMenu() {
            $.ajax({
                method: "get",
                async: true,
                url: '/api/Navigator/Getparentmenus',
                dataType: "json",
                success: function (res) {
                    if (null != res) {
                        var html = '';
                        html += '<option selected value="0">请选择，可为空</option>';
                        Object.keys(res).forEach(function (key) {
                            html += '<option value="' + res[key].id + '">' + res[key].name + '</option>';
                        });
                        $('#menuselect').html(html);
                        $('#menuselect').selectpicker('refresh');
                    }
                },
                error: function (e) {
                    toastr.error(e.responseText);
                }
            });
        }

        var TableInit = function () {
            var oTableInit = new Object();
            //初始化Table
            oTableInit.Init = function () {
                $('#TableMenu').bootstrapTable({
                    url: '/api/Navigator',                  //请求后台的URL（*）
                    method: 'get',                      //请求方式（*）
                    toolbar: '#toolbar',                //工具按钮用哪个容器
                    striped: true,                      //是否显示行间隔色
                    cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                    pagination: true,                   //是否显示分页（*）
                    sortable: false,                     //是否启用排序
                    sortOrder: "asc",                   //排序方式
                    queryParams: oTableInit.queryParams,//传递参数（*）
                    sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
                    pageNumber: 1,                       //初始化加载第一页，默认第一页
                    pageSize: 10,                       //每页的记录行数（*）
                    pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
                    search: false,                       //是否显示表格搜索，此搜索是客户端搜索，
                    strictSearch: true,
                    showColumns: true,                  //是否显示所有的列
                    showRefresh: true,                  //是否显示刷新按钮
                    minimumCountColumns: 2,             //最少允许的列数
                    clickToSelect: true,                //是否启用点击选中行
                    /*    height: 500,            */            //行高，如果没有设置height属性，表格自动根据记录条数决定表格高度
                    uniqueId: "id",                     //每一行的唯一标识，一般为主键列
                    showToggle: true,                    //是否显示详细视图和列表视图的切换按钮
                    cardView: false,                    //是否显示详细视图
                    detailView: false,
                    columns: [
                        {
                            checkbox: true
                        }, {
                            field: 'name',
                            title: '菜单名称',
                            align: 'center'
                        }, {
                            field: 'icon',
                            title: '图标',
                            align: 'center', formatter: function (value, row, index) {
                                return "<i class='" + value + "'></i>";
                            }
                        }, {
                            field: 'url',
                            title: '地址',
                            align: 'center'
                        }, {
                            field: 'no',
                            title: '排列序号',
                            align: 'center'
                        }, {
                            field: 'state',
                            title: '状态',
                            align: 'center', formatter: function (value, row, index) {
                                if (value != null) {
                                    if (value == 0) {
                                        return "<span class='label label-success'>正常</span>";
                                    }
                                    if (value == -1) {
                                        return "<span class='label label-errer'>禁用</span>";
                                    }
                                }
                            }
                        }, {
                            field: 'explain',
                            title: '说明',
                            align: 'center'
                        },
                        {
                            field: "agvId", title: "操作", width: 200, align: "center", formatter: function (value, row, index) {
                                menuArrar[row.id] = row;
                                var strHtml = '<button type="button" class="btn btn-primary btn-xs" onclick="edit(' + row.id + ')">编辑</button>&nbsp;' +
                                    '<button type="button" class="btn btn-danger btn-xs" onclick="deleteTool(' + row.id + ')">删除</button>';
                                return strHtml;
                            }, edit: false
                        }]
                });
            };
            //得到查询的参数
            oTableInit.queryParams = function (params) {
                var temp = {     //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                    limit: params.limit,   //页面大小
                    offset: params.offset,  //页码
                    meunName: $("#txt_menus_name").val()
                };
                return temp;
            };
            return oTableInit;
        };
        function DeleteSubmit(data) {
            $.ajax({
                url: '/api/Navigator',
                type: "delete",
                data: JSON.stringify(data),
                contentType: 'application/json',
                success: function (res) {
                    if (res.state == 200) {
                        $("#TableMenu").bootstrapTable('refresh');
                        toastr.success(res.message);
                    }
                    else {
                        $("#TableMenu").bootstrapTable('refresh');
                        toastr.error(res.message);
                    }
                },
                error: function (e) {
                    toastr.error(e.responseText);
                }
            });
        };

        function deletemenu(id) {
            bootbox.confirm({
                message: "确认删除吗？",
                buttons: {
                    confirm: {
                        label: '确认',
                        className: 'btn-primary'
                    },
                    cancel: {
                        label: '取消',
                        className: 'btn-default'
                    }
                },
                callback: function (result) {
                    if (result) {
                        DeleteSubmit([{ "id": id }]);
                    }
                }
            });
        }
        var menuArrar = new Array();

        function edit(index) {
            edittype = false;
            var menuInfo = menuArrar[index]
            if (menuInfo != null) {
                $('#myModalLabel').html('菜单编辑窗口');
                $('#icon').val(menuInfo.icon);
                $('#icon-menu').attr('class', "");
                $('#icon-menu').attr('class', menuInfo.icon);
                $('#name').val(menuInfo.name);
                $('#url').val(menuInfo.url);
                $('#order').val(menuInfo.no);
                $('#explain').val(menuInfo.explain);
                $('#menuID').val(menuInfo.id);
                $('#menuselect').selectpicker('val', menuInfo.menuid);
                $('#menutype').selectpicker('val', menuInfo.state);
                $('#dialogNew').modal();
            }
        }
    </script>
}
<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1 class="m-0 text-dark">菜单管理</h1>
            </div>
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item"><a href="@CoreConfiguration.DefaultMain">首页</a></li>
                    <li class="breadcrumb-item active">菜单管理</li>
                </ol>
            </div>
        </div>
    </div>
</div>
<div class="content">
    <div class="card">
        <div class="card-header">查询条件</div>
        <div class="card-body">

            <form class="form-inline">
                <div class="row">
                    <div class="form-group col-sm-6 col-lg-auto">
                        <label class="control-label" for="txt_menus_name">菜单名称</label>
                        <input type="text" class="form-control" id="txt_menus_name">
                    </div>
                    <div class="form-group col-sm-6 col-lg-auto flex-sm-fill justify-content-sm-end align-self-sm-end">
                        <button style="margin:3px" type="button" id="btn_query" class="btn btn-primary btn-fill"><i class="fa fa-search" aria-hidden="true"></i><span>查询</span></button>
                        <button style="margin:3px" type="button" id="btn_repeat" class="btn btn-primary btn-fill"><i class="fa fa-repeat" aria-hidden="true"></i><span>重置</span></button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div class="card">
        <div class="card-header">
            查询结果
        </div>
        <div class="card-body">
            <div class="alert alert-warning" role="alert">
                <span>请谨慎修改</span>
            </div>
            <div id="toolbar" class="btn-group">
                <div class="toolbar btn-group">
                    <button type="button" class="btn btn-info btn_add"><i class="fa fa-plus" aria-hidden="true"></i><span>新增</span></button>
                    <button type="button" class="btn btn-primary tb_edit"><i class="fa fa-pencil" aria-hidden="true"></i><span>编辑</span></button>
                    <button type="button" class="btn btn-danger btn_delete"><i class="fa fa-remove" aria-hidden="true"></i><span>删除</span></button>
                </div>
            </div>
            <table id="TableMenu"></table>
            @*</div>*@
            <div class="clearfix"></div>
        </div>
    </div>
</div>
@section modal {
    <div class="modal-header">
        <h5 class="modal-title" id="myModalLabel">菜单编辑窗口</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
    <div class="modal-body">
        <form class="form-inline" id="dataForm">
            <div class="form-row">
                <div class="form-group col-sm-6">
                    <input type="hidden" id="menuID" />
                    <label class="control-label" for="name">菜单名称</label>
                    <div class="input-group flex-sm-fill">
                        <input type="text" class="form-control" id="name" name="name" placeholder="不可为空，50字以内" maxlength="50" data-valid="true" />
                    </div>
                </div>
                <div class="form-group col-sm-6">
                    <label class="control-label" for="parentName">父级菜单</label>
                    <div class="input-group flex-sm-fill">
                        <select class="selectpicker selectc" id="menuselect" data-dropup-auto="false"></select>
                    </div>
                </div>
                <div class="form-group col-sm-6">
                    <label class="control-label" for="order">菜单序号</label>
                    <div class="input-group flex-sm-fill">
                        <input type="text" class="form-control digits" id="order" name="order" data-default-val="10" placeholder="不为空" maxlength="8" data-valid="true" />
                    </div>
                </div>
                <div class="form-group col-sm-6">
                    <label class="control-label" for="icon">菜单图标</label>
                    <div class="input-group flex-sm-fill">
                        <input type="text" class="form-control" id="icon" data-default-val="fa fa-fa" placeholder="请选择...(可为空)，50字以内" maxlength="50" />
                        <div class="input-group-append">
                            <button id="btnIcon" class="btn" type="button"><i class="" id="icon-menu"></i>选择</button>
                        </div>
                    </div>
                </div>
                <div class="form-group col-sm-6">
                    <label class="control-label" for="parentName">菜单路径</label>
                    <input type="text" class="form-control flex-sm-fill" id="url" placeholder="不可为空，默认#" data-valid="true" />
                </div>
                <div class="form-group col-sm-6">
                    <label class="control-label" for="parentName">菜单状态</label>
                    <div class="input-group flex-sm-fill">
                        <select class="selectpicker selectc" id="menutype" data-dropup-auto="false">
                            <option value="0">正常</option>
                            <option value="-1">禁用</option>
                        </select>
                    </div>
                </div>
                <div class="form-group col-12">
                    <label class="control-label" for="url">说明</label>
                    <input type="text" class="form-control flex-sm-fill" id="explain" placeholder="可为空" />
                </div>
            </div>
        </form>
    </div>
}

@section customModal{
    <div id="dialogIcon" class="modal-content icon-content">
        <div class="modal-header">
            <h5 class="modal-title" id="myIconModalLabel">请选择图标</h5>
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        </div>
        <div class="modal-body">
            <div class="loadIconView"><i class="fa fa-spinner fa-pulse"></i><span>正在加载图标...请稍等</span></div>
        </div>
        <div class="modal-footer">
            <span>被选择的图标</span><i class="fa fa-fa" id="pickIcon"></i>
            <button type="button" class="btn btn-secondary" data-dismiss="modal">
                <i class="fa fa-times"></i>
                <span>关闭</span>
            </button>
            <button type="button" class="btn btn-primary" id="btnSubmitIcon">
                <i class="fa fa-save"></i>
                <span>确定</span>
            </button>
        </div>
    </div>
}